package com.xiao.xin.comfyui.client.config;

import com.xiao.xin.core.Interceptor.WebscoketUserInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import java.util.List;

/***
 * 配置SpringWebSocket服务端
 */
@Configuration
@EnableWebSocketMessageBroker // 开启服务端配置
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /***
     * 配置服务端接受地址
     * /ws
     *
     *
     * 配置Http拦截器，为下面做铺垫
     *
     *
     * 配置跨域请求
     * * 表示所有
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws")
                .addInterceptors(new HttpSessionHandshakeInterceptor())
                .setAllowedOriginPatterns("*");
    }

    /***
     * 配置广播和对点
     * /topic 表示广播
     * /user 表示对点
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic","/user");
    }

    /***
     * 配置拦截器
     * WebscoketUserInterceptor
     * @param registry
     */
    @Override
    public void configureClientInboundChannel(ChannelRegistration registry) {
        registry.interceptors(new WebscoketUserInterceptor());
    }
}
